package com.zx.ishopping.mapper;

import com.zx.ishopping.VO.AddressVO;
import com.zx.ishopping.annotation.AutoFill;
import com.zx.ishopping.entity.Address;
import com.zx.ishopping.enums.OperationEnum;
import org.apache.ibatis.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Mapper
public interface AddressMapper {

    /**
     * 通过用户id获取地址
     * @param userId
     * @return
     */
    List<AddressVO> getAddress(Long userId);

    /**
     * 修改地址为非默认
     * @param 
     */
    @Update("update addresses set is_default = 0 where user_id = #{userId} and is_default = 1")
    void updateDefault(Long userId);

    /**
     * 添加地址
     * @param address
     */
    @AutoFill(value = OperationEnum.INSERT)
    @Insert("insert into addresses(user_id,name,phone,province,city,district,detail,is_default)" +
            " values(#{userId},#{name},#{phone},#{province},#{city},#{district},#{detail},#{isDefault})")
    void addAddress(Address address);

    /**
     * 删除地址
     * @param id
     * @param userId
     */
    @Delete("delete from addresses where id = #{id} and user_id = #{userId}")
    void deleteAddress(Long id, Long userId);

    /**
     * 修改地址
     * @param address
     */
    @AutoFill(value = OperationEnum.UPDATE)
    void updateAddress(Address address);

    /**
     * 根据id获取地址
     * @param addressId
     */
    @Select("select * from addresses where id = #{addressId}")
    Address getAddressById(Long addressId);
}
